package org.zhou.config;

import org.apache.tomcat.util.descriptor.LocalResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//重写方法
@Configuration
public class myLocaleResolver implements LocaleResolver {
//    解析请求

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
//        获取请求中的语言参数
        String language = request.getParameter("l");
        Locale locale = Locale.getDefault(); // 默认  如果没有自定义配置  那么就使用默认
//        System.out.println("获取参数");
        if (!StringUtils.isEmpty(language)){
            // 请求的参数携带了国际化参数 不为空 使用我们自己的配置
            String[] split = language.split("_");
            //国家 地区
          locale = new Locale(split[0], split[1]);
//            System.out.println("got it");

        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
